<?php

require 'simple_html_dom.php';
ini_set('max_execution_time', 999999999);
ini_set('max_input_time', 999999999);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("player_profile", $con);

function htmltrim($string){
	$string = trim($string);
	$pattern = '(?:[ \t\n\r\x0B\x00\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}]|&nbsp;|<br\s*\/?>)+';
	return preg_replace('/^' . $pattern . '|' . $pattern . '$/u', '', $string);
}

$count = 0;
function getBody($simple_html_node){
	if($simple_html_node->tag == "tbody"){
		return $simple_html_node;
	}
	if(sizeof($simple_html_node->children() > 0)){
		foreach($simple_html_node->children() as $node){
			$body = getBody($node);
			if( $body != null){
				return $body;
			}
		}
	}else{
		return null;
	}
}

function insertPlayer($title, $year, $player, $team){
	$type = "pro";
	$player = preg_replace("(\')", "\'", $player);
	$parts = explode(" ", $player);
	if(sizeof($parts) == 2){
		$first = $parts[0];
		$last = $parts[1];
	}
	else if(sizeof($parts) == 3){
		$first = $parts[0];
		$middle = $parts[1];
		$last = $parts[2];
	}
	
	
	$select = "SELECT id from players where first_name='$first' and last_name='$last'";
	echo "$select<br>";
	$result = mysql_query($select);
	if(mysql_num_rows($result) != 1){
		$select = "SELECT id from players where first_name='$first' and last_name='$last' and team like '%$team%'";
		//echo "$select<br>";
		$result = mysql_query($select);
		if(mysql_num_rows($result) == 0){
			$l = preg_replace("/([A-Z])/e", "strtolower('\\1')", $last);
			$select = "SELECT id from players where first_name='$first' and last_name='$middle $l' and team like '%$team%'";
			//echo "$select<br>";
			$result = mysql_query($select);
			if(mysql_num_rows($result) == 0){
				$m = preg_replace("/([A-Z])/e", "strtolower('\\1')", $middle);
				$select = "SELECT id from players where first_name='$first $m' and last_name='$last' and team like '%$team%'";
				//echo "$select<br>";
				$result = mysql_query($select);
				if(mysql_num_rows($result) == 0){
					$select = "SELECT id from players where nickname='$first' and last_name='$last'";
					//echo "$select<br>";
					$result = mysql_query($select);
				}
			}
		}
	}
	$r = mysql_fetch_assoc($result);
	$id = $r['id'];
	if($id == null){
		echo "<span style='color:red;'>Missing entry</span><br>";
	}
	$insert = "Insert into awards (award_title, year, player_id, type) VALUES ('$title', $year, $id, '$type')";
	mysql_query($insert);
	echo $insert."<br>";
	
}

function getAwardList($title, $href){
	$base_url = "http://www.pro-football-reference.com/players/";
	$url = $base_url.$href;
	$page = file_get_html($url);
	$winners = $page->find("table.stats_table tbody", 0);//->children(2);
	$tbody = getBody($winners);
	$winners = $tbody->children();
	foreach($winners as $row){
		$values = $row->find("td");
		$year = $values[0]->plaintext;
		$player = $values[2]->plaintext;
		$team = $values[3]->plaintext;
		echo "<div>$year $player $team</div>";
		insertPlayer($title, $year, $player, $team);
	}
}

$awards_url = "http://www.pro-football-reference.com/players/award_master_list.htm";

$awards_page = file_get_html($awards_url);

$awardsList = $awards_page->find("div.#page_content ul li");
//echo var_dump($awardsList)."<br>";
foreach($awardsList as $award){
	$link = $award->find("a", 0);
	$title = $link->plaintext;
	echo $link->plaintext."<br>";
	$href = $link->href;
	getAwardList($title, $href);
}


?>